package com.dhcc.bpm.modules.metadata.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.modules.metadata.entity.ReqMetadata;
import com.dhcc.bpm.modules.metadata.service.IReqMetadataService;
import com.dhcc.bpm.modules.metadata.vo.ReqMetadataVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName ReqMetadataController
 * @Description 请求元数据关联表 控制器
 * @Author daina
 * @Date 2020/2/21 14:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 */
@RestController
@AllArgsConstructor
@RequestMapping("bpm-metadata/reqmetadata")
@Api(value = "请求元数据关联表", tags = "请求元数据关联表接口")
public class ReqMetadataController extends BladeController {

	private IReqMetadataService reqMetadataService;
	/**
	 * @Description 批量取消
	 * @Author daina
	 * @Date 10:47 2020/2/22
	 * @param opcApiIdReq
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/deleteReqMetadata")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	@ApiLog("批量取消请求参数信息")
	@Log(title = "批量取消请求参数信息", businessType = BusinessType.DELETE)
	public R deleteReqMetadata(@ApiParam(name ="opcApiIdReq", value = "网关id") @RequestParam Long  opcApiIdReq,@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		int apis = reqMetadataService.deleteReqMetadata(opcApiIdReq, ids);
		if(apis>0){
			return  R.status(true);
		}else{
			return  R.status(false);
		}
	}
	/**
	 * @Description 请求参数批量添加元数据
	 * @Author daina
	 * @Date 10:47 2020/2/22
	 * @param opcApiIdReq
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/addReqMetadata")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "添加", notes = "opcApiIdReq,ids")
	@ApiLog("接口请求参数批量添加元数据")
	@Log(title = "接口请求参数批量添加元数据", businessType = BusinessType.INSERT)
	public R addReqMetadata(@ApiParam(name ="opcApiIdReq", value = "接口id") @RequestParam Long  opcApiIdReq,@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		List<Long> metaIds = Func.toLongList(ids);
		ArrayList<ReqMetadata> list = new ArrayList<>();
		metaIds.forEach(metaId->{
			ReqMetadata req = new ReqMetadata();
			req.setOpcApiIdReq(opcApiIdReq);
			req.setOpcMetadataId(metaId);
			list.add(req);
		});
		return  R.status(reqMetadataService.saveBatch(list));
	}
	/**
	 * @Description 详情
	 * @Author daina
	 * @Date 10:47 2020/2/22
	 * @param reqMetadata
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.metadata.entity.ReqMetadata>
	 **/
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入reqMetadata")
	@ApiLog("详情")
	@Log(title = "详情",businessType = BusinessType.SELECT)
	public R<ReqMetadata> detail(ReqMetadata reqMetadata) {
		ReqMetadata detail = reqMetadataService.getOne(Condition.getQueryWrapper(reqMetadata));
		return R.data(detail);
	}

	/**
	 * @Description 分页 请求元数据关联表
	 * @Author daina
	 * @Date 10:47 2020/2/22
	 * @param reqMetadata
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.metadata.entity.ReqMetadata>>
	 **/
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入reqMetadata")
	@ApiLog("分页 请求元数据关联表")
	@Log(title = "分页 请求元数据关联表",businessType = BusinessType.SELECT)
	public R<IPage<ReqMetadata>> list(ReqMetadata reqMetadata, Query query) {
		IPage<ReqMetadata> pages = reqMetadataService.page(Condition.getPage(query), Condition.getQueryWrapper(reqMetadata));
		return R.data(pages);
	}

	/**
	 * @Description 自定义分页 请求元数据关联表
	 * @Author daina
	 * @Date 10:47 2020/2/22
	 * @param reqMetadata
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.metadata.vo.ReqMetadataVO>>
	 **/
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入reqMetadata")
	@ApiLog("自定义分页 请求元数据关联表")
	@Log(title = "自定义分页 请求元数据关联表",businessType = BusinessType.SELECT)
	public R<IPage<ReqMetadataVO>> page(ReqMetadataVO reqMetadata, Query query) {
		IPage<ReqMetadataVO> pages = reqMetadataService.selectReqMetadataPage(Condition.getPage(query), reqMetadata);
		return R.data(pages);
	}

	/**
	 * @Description 新增 请求元数据关联表
	 * @Author daina
	 * @Date 10:47 2020/2/22
	 * @param reqMetadata
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入reqMetadata")
	@ApiLog("新增请求元数据关联表")
	@Log(title = "新增",businessType = BusinessType.INSERT)
	public R save(@Valid @RequestBody ReqMetadata reqMetadata) {
		return R.status(reqMetadataService.save(reqMetadata));
	}

	/**
	 * @Description 修改 请求元数据关联表
	 * @Author daina
	 * @Date 10:47 2020/2/22
	 * @param reqMetadata
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入reqMetadata")
	@Log(title = "修改",businessType = BusinessType.UPDATE)
	@ApiLog("修改")
	public R update(@Valid @RequestBody ReqMetadata reqMetadata) {
		return R.status(reqMetadataService.updateById(reqMetadata));
	}

	/**
	 * @Description 新增或修改 请求元数据关联表
	 * @Author daina
	 * @Date 10:48 2020/2/22
	 * @param reqMetadata
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入reqMetadata")
	@Log(title = "修改",businessType = BusinessType.UPDATE)
	@ApiLog("修改")
	public R submit(@Valid @RequestBody ReqMetadata reqMetadata) {
		return R.status(reqMetadataService.saveOrUpdate(reqMetadata));
	}

	
	/**
	 * @Description 删除 请求元数据关联表
	 * @Author daina
	 * @Date 10:48 2020/2/22
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/remove")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "删除", notes = "传入ids")
	@ApiLog("删除")
	@Log(title = "删除",businessType = BusinessType.DELETE)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(reqMetadataService.removeByIds(Func.toLongList(ids)));
	}

	
}
